package cease.butter;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import net.sf.json.JSON;

import org.apache.commons.lang.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import cease.console.PrintHelper;
import cease.urs.LoginException;
import cease.urs.Loginer;
import cease.urs.Session;
import cease.vo.Message;

/**
 * @author dhf
 */
public class MessageExecutor extends BaseExecutor {
    private static MessageExecutor executor = new MessageExecutor();

    public static MessageExecutor getInstance() {
        return executor;
    }

    public Collection<Message> showMessages(Session session, Integer size,
            String cursor) throws ApiException {
        if (null != size) {
            if (size <= 0) {
                size = 1;
            }
            if (size > 100) {
                size = 100;
            }
        }
        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        if (null != size) {
            pairs.add(new BasicNameValuePair("count", size + ""));
        }
        if (null != cursor) {
            pairs.add(new BasicNameValuePair("since_id", cursor));
        }
        pairs.add(new BasicNameValuePair("t", System.currentTimeMillis() + ""));
        JSON json = get(Constants.URL_MESSAGE_SHOW, pairs, null, session);
        return toMessageArray(json);
    }

    public Message create(Session session, String nickName, String text)
            throws ApiException {
        if (StringUtils.isBlank(nickName)) {
            throw new IllegalArgumentException("[nickName] could not be null");
        }
        if (StringUtils.isBlank(text)) {
            throw new IllegalArgumentException("[text] could not be null");
        }
        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        pairs.add(new BasicNameValuePair("nickName", nickName));
        pairs.add(new BasicNameValuePair("text", text));
        // XXX 1. 如果指定的nickname不存在或者跟你不是互相关注的，会只返回null（非json格式）
        // XXX 2.
        // api异常堆栈信息牛头不对马嘴，api文档中明明是用id（用户id或email）指定接收者，但是抛出的异常堆栈却提示message不能为null
        JSON json = post(Constants.URL_MESSAGE_CREATE, pairs, null, session);
        return toMessage(json);
    }

    public Message delete(Session session, String messageId)
            throws ApiException {
        if (StringUtils.isBlank(messageId)) {
            throw new IllegalArgumentException("[messageId] could not be null");
        }

        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        pairs.add(new BasicNameValuePair("id", messageId));
        JSON json = post(Constants.URL_MESSAGE_DELETE, pairs, null, session);
        return toMessage(json);
    }

    public static void main(String[] args) throws LoginException, ApiException {
        Session session = Loginer.login("dhf@126.com", "wrongpassword");
        MessageExecutor executor = new MessageExecutor();
        PrintHelper.printMessage(executor.showMessages(session, null, null));
    }
}
